Skip to main content

Function methods

Functions

A function is a set of statements that perform a task together. Every C++ program has at least one function, the main function main() , and all simple programs can define other additional functions.

You can divide the code into different functions. How you divide the code into different functions is up to you, but logically the division is usually based on each function performing a specific task.

The function declaration tells the compiler the name, return type and parameters of the function. The function definition provides the actual body of the function.

Definitions

return_type function_name( parameter list )
{
body of the function
}
  • return_type: A function can return a value. return_type is the data type of the value returned by the function. Some functions perform the required operation without returning a value, in which case return_type is the keyword void.
  • function_name: This is the actual name of the function. Together with the parameter list, the function name forms the function signature.
  • parameter list: Parameters are like placeholders. When a function is called, you pass a value to the parameter, which is called the actual parameter. The parameter list includes the type, order and number of the function's parameters. Arguments are optional, i.e. the function may not contain parameters.
  • The body of the function (body): The body of a function contains a set of statements that define the task the function performs.

Example

// The function returns the larger of two numbers

int max(int num1, int num2)
{
// local variable declaration
int result;

if (num1 > num2)
result = num1;
else
result = num2;

return result;
}

#include <iostream>
using namespace std;

// function declaration
int max(int num1, int num2);

int main ()
{
// local variable declaration
int a = 100;
int b = 200;
int ret;

// call the function to get the maximum value
ret = max(a, b);

cout << "Max value is : " << ret << endl;

return 0;
}

// function returns the larger of two numbers
int max(int num1, int num2)
{
// local variable declaration
int result;

if (num1 > num2)
result = num1;
else
result = num2;

return result;
}

Parameters

Function parameters

If a function is to use arguments, it must declare variables that accept the values of the arguments. These variables are called the formal parameters of the function.

Formal parameters are like other local variables within the function, created on entry to the function and destroyed on exit.

When a function is called, there are two ways of passing parameters to the function.

call typedescription
Passing a value callThis method copies the actual value of the argument to the formal parameter of the function. In this case, modifying the formal argument within the function has no effect on the actual argument.
Pointer callThis method copies the address of the argument to the formal parameter. Within the function, this address is used to access the actual parameter to be used in the call. This means that modifying the formal parameter will affect the actual parameter.
Reference callThis method copies a reference to the argument to the formal parameter. Within the function, the reference is used to access the actual parameter to be used in the call. This means that modifying the formal parameter affects the actual parameter.

By default, C++ uses passing calls to pass arguments. In general, this means that the code within the function cannot change the arguments used to call the function. The previously mentioned example, calling the max() function, uses the same approach.

Passing value calls

The pass-value-call method of passing arguments to a function copies the actual value of the argument to the formal argument of the function.

// function definition
void swap(int x, int y)
{
int temp;

temp = x; /* save the value of x */
x = y; /* assign y to x */
y = temp; /* assign x to y */

return;
}

// Instance
#include <iostream>
using namespace std;

// function declaration
void swap(int x, int y);

int main ()
{
// local variable declaration
int a = 100;
int b = 200;

cout << "交换前,a 的值:" << a << endl;
cout << "交换前,b 的值:" << b << endl;

// Calling a function to exchange values
swap(a, b);

cout << "交换后,a 的值:" << a << endl;
cout << "交换后,b 的值:" << b << endl;

return 0;
}

// Output
交换前,a 的值: 100
交换前,b 的值: 200
交换后,a 的值: 100
交换后,b 的值: 200

Pointer calls

The pointer call method that passes an argument to a function copies the address of the argument to the formal parameter.

// function definition
void swap(int *x, int *y)
{
int temp;
temp = *x; /* save the value of address x */
*x = *y; /* assign y to x */
*y = temp; /* assign x to y */

return;
}

// Instance
#include <iostream>
using namespace std;

// function declaration
void swap(int *x, int *y);

int main ()
{
// local variable declaration
int a = 100;
int b = 200;

cout << "交换前,a 的值:" << a << endl;
cout << "交换前,b 的值:" << b << endl;

/* Calling a function to exchange values
* &a indicates a pointer to a, i.e. the address of variable a
* &b denotes a pointer to b, i.e. the address of variable b
*/
swap(&a, &b);

cout << "交换后,a 的值:" << a << endl;
cout << "交换后,b 的值:" << b << endl;

return 0;
}

// Output
交换前,a 的值: 100
交换前,b 的值: 200
交换后,a 的值: 200
交换后,b 的值: 100

Reference calls

The reference call method that passes an argument to a function copies the address of the reference to the formal argument.

// function definition
void swap(int &x, int &y)
{
int temp;
temp = x; /* save the value of address x */
x = y; /* assign y to x */
y = temp; /* assign x to y */

return;
}

// Instance
#include <iostream>
using namespace std;

// function declaration
void swap(int &x, int &y);

int main ()
{
// local variable declaration
int a = 100;
int b = 200;

cout << "交换前,a 的值:" << a << endl;
cout << "交换前,b 的值:" << b << endl;

/* Calling a function to exchange values */
swap(a, b);

cout << "交换后,a 的值:" << a << endl;
cout << "交换后,b 的值:" << b << endl;

return 0;
}

// Output
交换前,a 的值: 100
交换前,b 的值: 200
交换后,a 的值: 200
交换后,b 的值: 100

Default values

When you define a function, you can specify a default value for each of the arguments that follow in the argument list. When the function is called, this default value is used if the value of the actual argument is left blank.

#include <iostream>
using namespace std;

int sum(int a, int b=20)
{
int result;

result = a + b;

return (result);
}

int main ()
{
// Local variable declaration
int a = 100;
int b = 200;
int result;

// call the function to add the value
result = sum(a, b);
cout << "Total value is :" << result << endl;

// call the function again
result = sum(a);
cout << "Total value is :" << result << endl;

return 0;
}

// Output
Total value is :300
Total value is :120

Lambda expressions

Lambda expressions treat functions as objects, and can be used like objects, for example by assigning them to variables and passing them as arguments, and by evaluating them like functions.

// Syntax
[capture](parameters)->return-type{body}

// Instances
[](int x, int y){ return x < y ; }